<?php


namespace app\manage\controller;
use app\common\model\Business as BusinessModel;
use app\common\model\OrderGoods;
use app\facade\Business as BusinessFacade;
use app\common\model\Members as MembersModel;
use app\common\model\Orders as OrdersModel;
use app\common\model\Goods as GoodsModel;
class Business extends Base
{
    //商家审核
    public function index(){
        $data = BusinessModel::where("status",BusinessModel::STATUS_NO)->paginate(10);

        return view('',[
            'data'  => $data,
            'count' => $data->total(),
            'page'  => $data->currentPage(),
            'limit' => $data->listRows(),
        ]);
    }
    //查看商家
    public function show(){
        $business_id = input('business_id');
        $data = BusinessModel::get($business_id);
        return view('/business_show',[
            'data' => $data
        ]);
    }
    //商家审核
    public function examine(){
        $id = input('id');
        $type = input('type');
        $remarks = input('remarks');
        $data = BusinessModel::get($id);
        if ($type == "yes"){
            if (!BusinessFacade::becomeBusiness($data->member_id)){
                return j('1','操作失败');
            }
            $data->status = BusinessModel::STATUS_YES;
        }else{
            $data->status = BusinessModel::STATUS_FAIL;
            $data->remarks = $remarks;
        }
        if ($data->save()){
            return j('0','操作成功');
        }else{
            return j('1','操作失败');
        }
    }
    //商家推荐
    public function recommend(){
        $data = BusinessModel::where("status",BusinessModel::STATUS_YES)->paginate(10);

        return view('/business_recommend',[
            'data'  => $data,
            'count' => $data->total(),
            'page'  => $data->currentPage(),
            'limit' => $data->listRows(),
        ]);
    }
    //客户信息
    public function customerInfo(){
        $userid = input('userid');
        $data = MembersModel::where("parent_id",$userid)->paginate(10);
        return view('/distribution_users_customer_info',[
            'data' => $data,
            'count' => $data->total(),
            'page' => $data->currentPage(),
            'limit' => $data->listRows(),
        ]);
    }
    //消费记录
    public function consumptionLog(){
        $where  = $this->consumptionLogWhere();
        $data = OrdersModel::where($where)->paginate(10);

        return view('/consumption_log',[
            'data' => $data,
            'count' => $data->total(),
            'page' => $data->currentPage(),
            'limit' => $data->listRows(),
        ]);
    }
    //销售统计
    public function saleData(){
        $userid = input('userid');
        $memberData = MembersModel::where("parent_id",$userid)->select();
        $member_id = [];
        $orders_id = [];
        foreach ($memberData as $key =>$value){
            $member_id[] = $value['id'];
        }

        $ordersData = OrdersModel::whereIn("userid",$member_id)->where('order_status',255)->field('id')->select();
        foreach ($ordersData as $key =>$value){
            $orders_id[] = $value['id'];
        }
        $goodsData = GoodsModel::where("is_sale",1)->field('id,goods_name')->select();
        foreach ($goodsData as $key => $value){
           if ($ordersData->isEmpty()){
               $goodsData[$key]['count'] = 0;
           }else{
               $goodsData[$key]['count'] = OrderGoods::whereIn("order_id",$orders_id)->where("goods_id",$value['id'])->count(['id']);
           }

        }
        return view('/sale_data',[
            'goods' => $goodsData
        ]);
    }
    //用户分配
    public function memberDistribution(){
        $data = MembersModel::where("parent_id",0)->where("is_business",0)->paginate(10);
        return view('/member_distribution',[
            'data' => $data,
            'count' => $data->total(),
            'page' => $data->currentPage(),
            'limit' => $data->listRows(),
        ]);
    }
    //用户分配 分配页面
    public function distribution(){
        $member_id =  input('member_id');
        $memberData = MembersModel::field('id,nickname')->get($member_id);
        $businessData = BusinessModel::where("status",BusinessModel::STATUS_YES)->field('id,business_name')->select();
        return view('/business_member_distribution',[
            'businessData' => $businessData,
            'memberData' => $memberData
        ]);
    }
    //用户分配 提交
    public function saveDistribution(){
        $member_id = input('member_id');
        $business_id = input('business_id');
        $business = BusinessModel::get($business_id);
        if ($business->member_id == $member_id){
            return j(1,'不能选择自己商家作为上级');
        }
        $member = MembersModel::get($member_id);
        $member->parent_id = $business->member_id;
        if ($member->save()){
            return  j(0,'分配成功');
        }else{
            return  j(1,'分配失败');
        }
    }
    //销售记录条件
    public function consumptionLogWhere()
    {

        $userid = input('userid');


        $order_status_id = input('get.order_status_id/d', 0);

        $keywords        = input('get.keywords/s', '');

        $startTime       = input('get.start_time/s', '');

        $endTime         = input('get.end_time/s', '');

        $where           = [];

        $member_id       = [];

        $memberData = MembersModel::where("parent_id",$userid)->field('id')->select();

        foreach ($memberData as $key => $value){
            $member_id[] = $value['id'];
        }

        $where[] = [
            ['userid', 'in', $member_id],
        ];

        if ($order_status_id) {

            switch ($order_status_id) {

                case 1:

                    // 已取消

                    $where[] = [

                        ['order_status', 'eq', 2],

                    ];

                    break;

                case 2:

                    // 未付款

                    $where[] = [

                        ['order_status', 'eq', 1],

                        ['pay_status', 'eq', 0],

                    ];

                    break;

                case 3:

                    // 已付款

                    $where[] = [

                        ['order_status', 'eq', 1],

                        ['pay_status', 'eq', 1],

                    ];

                    break;

                case 4:

                    // 待发货

                    $where[] = [

                        ['order_status', 'eq', 1],

                        ['pay_status', 'eq', 1],

                        ['shipping_status', 'eq', 0],

                    ];

                    break;

                case 5:

                    // 待收货

                    $where[] = [

                        ['order_status', 'eq', 1],

                        ['pay_status', 'eq', 1],

                        ['shipping_status', 'eq', 1],

                    ];

                    break;

                case 6:

                    $where[] = [

                        ['order_status', 'eq', 255],

                    ];

                    // 已完成

                    break;

            }

        }



        if ($keywords) {

            $where[] = [

                ['username', 'like', '%'.$keywords.'%'],

            ];

        }



        if ($startTime) {

            $where[] = [

                ['create_time', 'egt', strtotime($startTime)],

            ];

        }

        if ($endTime) {

            $where[] = [

                ['create_time', 'lt', strtotime($endTime)],

            ];

        }



        $this->assign('order_status_id', $order_status_id);

        $this->assign('keywords', $keywords);

        $this->assign('startTime', $startTime);

        $this->assign('endTime', $endTime);
        $this->assign('userid', $userid);

        return $where;

    }
}